"""
The very import Snowball...
$Id: snowball.py,v 1.6 2004/12/17 09:57:50 krawczyk Exp $


global snowballimgs -- list of all snowball images
"""

import pygame, random, math
import config, basic_drawables

class Snowball(pygame.sprite.Sprite):
    """
    Snowball class
    """
    
    gravity = 0.3265
    maxhits = config.game["snowball.maxhits"]
    
    def __init__(self, group, size, pos, speed):
        """Initialises the Snowballclass:
        size -- Size of snowball is in [0..1]
        pos -- starting position
        speed -- starting speed
        """
        
        pygame.sprite.Sprite.__init__(self, group)

        GAME = config.game
        IMGS = snowballimgs
        #int(snowp*config.game["snowball.maxsize"])
        
        if size < 0:
            print "Warning! You tried to create a snowball with an illegal size of ", size
            size = 0
        self.size = size
        if size >= 1:
            size = .9999
        self.image = IMGS[int(size*(len(IMGS)))]
        self.image_rect = self.image.get_rect()
        self.rect = self.image_rect
        self.x, self.y = pos
        self.hspeed, self.vspeed = speed
        self.time2detonate = -self.vspeed/2/self.gravity+math.sqrt((self.vspeed/2/self.gravity)**2-(self.y-config.game["snowball.vanishes_Y"])/self.gravity)
        self.time2detonate /= .56 #Why this correction factor???
        self.detonate_x = self.x + self.hspeed*self.time2detonate
        print speed, self.time2detonate, self.detonate_x
        self._update_pos()

    def _update_pos(self):
        self.rect[0:2] = self.x, self.y

    def Pos(self):
        return (self.x, self.y)
    
    def update(self):
        self.vspeed += self.gravity
        self.x += self.hspeed
        self.y += self.vspeed
        if self.y >= config.game["snowball.vanishes_Y"] or self.x >= config.scrsize[0] or self.x < 0:
            print 'should detonante @',self.detonate_x,'has detonated @',self.x,self.detonate_x/self.x
            self.kill()
        else:
            self._update_pos()


class Snowball_slow(Snowball):
    """
    A slow snowball class. Just for fun this snowball moves (very) slowly.
    """
    def __init__(self, group, size, pos, speed, frames_to_skip=5):
        """Initialises the Snowballclass:
        size -- Size of snowball is in [0..1]
        pos -- starting position
        speed -- starting speed
        frames_to_skip -- number of frames to skip, before any update happens.
        """
        Snowball.__init__(self, group, size, pos, speed)
        self.counter = 0
        self.frames_to_skip = frames_to_skip
        self.y -= 5

    def update(self):
        self.counter += 1
        if self.counter >= self.frames_to_skip:
            self.counter = 0
            Snowball.update(self)


class Snowball_tumbling(Snowball):
    """
    A slow snowball class. Just for fun this snowball moves wildly.
    """
    def __init__(self, group, size, pos, speed):
        """Initialises the Snowballclass:
        size -- Size of snowball is in [0..1]
        pos -- starting position
        speed -- starting speed
        """
        Snowball.__init__(self, group, size, pos, speed)
        self.f = 0
        self.rand_x = random.randint(-12, 12)
        self.rand_y = random.randint(3, 7)

    def update(self):
        self.f += 1
        self.x += math.sin(self.f) * self.rand_x
        self.y -= math.sin(self.f) * self.rand_y
        Snowball.update(self)

def Init():
    global snowballimgs
    config.Status_top("Initialising Snowballs...")
    #Load Image files
    imgs = []
    game = config.game
    size = len(game["snowballimgnames"])
    print "Loading snowball images:",
    for i in game["snowballimgnames"]:
        print i,
        imgs.append(pygame.image.load(i))
    print
    assert len(imgs) == size
    snowballimgs = imgs
